package model.vo
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import event.MaperEvent;
	
	import net.NetLoader;
	
	import utils.UIDUtil;

	/**
	 * @author : LIMIN
	 * @time : 2013-5-16 上午9:52:46
	 * 
	 * @remarks :
	 */
	
	public class DeviceStatusVO
	{
		private var _statusColor:uint;
		public function get statusColor():uint
		{
			return _statusColor;
		}
		private var _statusStr:String;
		public function get statusStr():String
		{
			return _statusStr;
		}
		
		private var _oneDay:Number = 1000*60*60*24;//一天的毫秒数
		private var _warningDays:int = 5;//即将到期的警告天数
		private var _tenMinute:int = 1000*60*10;//10分钟
		
		private var _data:Object;
		
		/*
		设备状态:
		1.欠费,0xFF9900
		2.即将到期,0xFFFF00,5天之内
		3.停机,0xCCCCCC
		4.报警,0xFF0000,提示报什么警,和设备报警列表中报警类型提示的一样
		5.GPS未定位,0x00FF00
		6.正常,0x000000
		7.基站定位,0x0000FF
		8.设备未上传,0xFF0000
		9.未有新数据, 颜色同"正常"or"未定位"
		*/
		
		/*
		判断逻辑:
		是否需要服务:
		否:
		停机[3]
		欠费[1]
		是:
		是否有警报:
		是:显示警报[4]
		否:
		判断设备状态:
		设备未上传[8]
		未有新数据[9]
		基站定位[7]
		GPS未定位[5]
		即将到期[2]
		正常[6]
		*/
		
		public function DeviceStatusVO(obj:Object)
		{
			_data = obj;
			switchStatus(obj);
		}
		
		/** 分析状态数据 **/
		private function switchStatus(obj:Object):void
		{
			if(obj.status==1)//停机[3]
			{
				_statusColor = 0xCCCCCC;
				_statusStr = "停机";
			}else if(isExpire(Global.getTimeByTimeStr(obj.expire_date,true)))//欠费[1]
			{
				_statusColor = 0xFF9900;
				_statusStr = "欠费";
			}else
			{
				switchAlarmStatus(obj);
			}
		}
		
		/** 警报状态 **/
		private function switchAlarmStatus(obj:Object):void
		{
			if(obj.status==-2)//警报[4]
			{
				_statusColor = 0xFF0000;
				_statusStr = "报警";
//				if(obj.position)
//				{
//					_statusStr = obj.position.map.status_text;
//				}
				//LMTODO
//				getMemberDeviceByNode(obj.device_id);
//				getPositionData(obj);
			}else if(!obj.position)//设备未上传[8]
			{
				_statusColor = 0xFF0000;
				_statusStr = "设备未上传";
			}else if(isOldPosition(obj.position.map.dateline))//未有新数据[9]
			{
				if(obj.type_id==19 && obj.position.map.is_valided==1)//基站定位
				{
					_statusColor = 0x0000FF;
				}else if((obj.type_id==1||obj.type_id==12||obj.type_id==15) && obj.position.map.is_valided==1)//GPS未定位
				{
					_statusColor = 0x00FF00;
				}else
				{
					_statusColor = 0x000000;
				}
				_statusStr = "未有新数据";
			}else//是新数据
			{
				if(obj.type_id==19 && obj.position.map.is_valided==1)//基站定位[7]
				{
					_statusColor = 0x0000FF;
					_statusStr = "基站定位";
				}else if((obj.type_id==1||obj.type_id==12||obj.type_id==15) && obj.position.map.is_valided==1)//GPS未定位[5]
				{
					_statusColor = 0x00FF00;
					_statusStr = "GPS未定位";
				}else
				{
					switchWarningStatus(obj);
				}
			}
		}
		
		private function switchWarningStatus(obj:Object):void
		{
			if(isExpireWarning(Global.getTimeByTimeStr(obj.expire_date,false)))//即将到期[2]
			{
				_statusColor = 0xFFFF00;
				_statusStr = "即将到期";
			}else//正常[6]
			{
				_statusColor = 0x000000;
				_statusStr = "正常";
			}
		}
		
		/** 是否到期 **/
		private function isExpire(expireTime:Number):Boolean
		{
			return Global.systemTime > expireTime;
		}
		
		/** 是否即将到期 **/
		private function isExpireWarning(expireTime:Number):Boolean
		{
			return Global.systemTime > expireTime-_oneDay*_warningDays;
		}
		
		/** 是否旧数据(10) **/
		private function isOldPosition(dateline:Number):Boolean
		{
			return Global.systemTime > dateline*1000+_tenMinute;
		}
		
		//
		public function getMemberDeviceByNode(id:int):void
		{
			var loader:NetLoader;
			loader = new NetLoader();
			
			var url:String = Global.getServerPathByAction(ActionConst.GET_MEMBER_DEVICES);
//			url += "&type=" + Global.deviceType;
//			url += "&node=" + node;
//			url += "&page=" + 1;
//			if(!isMember)
//			{
//				url += "&perpage=100";
//			}else
//			{
//				url += "&perpage=" + int(nodeCountData[node]);
//			}
			url += "&id[]=" + id;
			loader.load(url,onMemberDeviceCompleteHandler);
			
			Global.openLoading();
		}
		
		private function onMemberDeviceCompleteHandler(data:Object):void
		{
			if(data.status == 1)
			{
				//memberNodes = data.data;//menuPanel.setMList();
				Global.deviceAllArray = new ArrayCollection(UIDUtil.update(data.data));
			}else
			{
				Alert.show("此节点暂时没有设备");
				Global.deviceAllArray= new ArrayCollection();
				
			}
//			setDeviceArrayByType();
//			//				cursorManager.removeBusyCursor();
//			Global.hideLoading();
//			
//			updateDeviceDataProvider();
		}
		
		
		//
		
		protected function getPositionData(currentDeviceData:Object):void
		{
			var loader:NetLoader;
			loader = new NetLoader();
			
//			Global.currentDeviceInfo = currentDeviceData;
			var eDateObject:Date = new Date(Global.systemTime);
			var sDateObject:Date = new Date(eDateObject.getTime() - 24*60*60*1000);
			
			var url:String = Global.getServerPathByAction(ActionConst.GET_POSITION_DATA);
			url += "&id=" + (currentDeviceData.device_id || currentDeviceData.client_id);
			url += "&begin_date=" + sDateObject.getFullYear() + "-" + (sDateObject.getMonth()+1) + "-" + sDateObject.getDate();
			url += "&end_date=" + eDateObject.getFullYear() + "-" + (eDateObject.getMonth()+1) + "-" + eDateObject.getDate();
			url += "&hour_begin=" + sDateObject.getHours();
			url += "&minute_begin=" + sDateObject.getMinutes();
			url += "&hour_end=" + eDateObject.getHours();
			url += "&minute_end=" + eDateObject.getMinutes();
			
			loader.load(url,onPositionDataCompleteHandler);
			Global.openLoading();
		}
		
		private function onPositionDataCompleteHandler(data:Object):void
		{
			Global.hideLoading();
			
			if( data == null)
			{
				return ;
			}
			
			if(data.status == 1)
			{
//				dispatchEvent(new MaperEvent(MaperEvent.DISPLAY_CURRENT_DEVICE_LOCATION,data.data));
			}else
			{
//				var errorStr:String;
//				switch(data.status)
//				{
//					case -1000:
//						errorStr = "尚未登录用户，请登录";
//						break;
//					case -1:
//						errorStr = "设备id未知";
//						break;
//					case -2:
//						errorStr = "禁止或无权限查看数据";
//						break;
//					case -3:
//						errorStr = "数据表不存在";
//						break;
//					case -4:
//						errorStr = "时间不合法";
//						break;
//					case -5:
//						errorStr = "起始时间超过结束时间";
//						break;
//					default:
//						errorStr = "24小时内没有定位信息";
//						break;
//				}
//				
//				Alert.show(errorStr,"获取数据失败");
			}
		}
		
	}
}